<!-- <h3 class="page-title">机构分配</h3> -->
<div class="row">
	<div class="col-md-12">
		<!-- BASIC TABLE -->
		<div class="panel">
			<div class="panel-heading">
				<form class="form-inline" id="searchForm"></form>
			</div>
			<div class="panel-body  clearfix">
				<div class="manage_list clearfix" >
					<ul id="tree" class="ztree"></ul>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript" >
		
		var checkNo = null;//选中的节点
		
		var setting1 = {
				view: {
					selectedMulti: false,
					dblClickExpand: false,
				},
				data:{
					simpleData:{
						enable:true,
						idKey:"id",
						icon:"iconSkin",
						pIdKey:"pid"
					},
					key:{
						name:"name"
					}
				},
				callback: {
					onClick: this.menuClick
				}
			};
		
		//节点点击事件
		function menuClick(e,treeId,node){
			checkNo = node;		    	
	    }
		
		
		$(document).ready(function(){
			
			var zNode1;
			$.ajax({
				async:false,
				cache:false,
				type:'POST',  
	        	dataType:"json",  
				url:'/org/initOrg',
				success:function(data){
					zNode1 = data;
				}
			});
			for(var i in zNode1){
				zNode1[i].url = null;
			}
			$.fn.zTree.init($("#tree"), setting1, zNode1);
	
		});
		
		//添加节点
		function addNode(newNode) {
			
			newNode.url = null;
			var zTree = $.fn.zTree.getZTreeObj("tree");
            if (zTree.getSelectedNodes()[0]) {  
                zTree.addNodes(zTree.getSelectedNodes()[0], newNode);  
            } else {  
                zTree.addNodes(null, newNode);  
            }  
        }  
		
		//修改节点
		function editTree(newNode){
		
			newNode.url = null;
			var zTree = $.fn.zTree.getZTreeObj("tree");
			var node = zTree.getSelectedNodes()[0];  
			node.name = newNode.name;
			node.remark = newNode.remark;
			zTree.updateNode(node);
		}
		
		//删除节点
		function removeNode() {  
			var zTree = $.fn.zTree.getZTreeObj("tree");
            var nodes = zTree.getSelectedNodes();  
            if (nodes && nodes.length>0) {  
                checkNo = null;//选中的节点归零
                if (nodes[0].children && nodes[0].children.length > 0) {  
                    zTree.removeNode(nodes[0]);  
                } else {  
                    zTree.removeNode(nodes[0]);  
                }  
            }  
        } 
</script>

<script>
	$(function(){
		
		 $(".panel-heading form").createToolBar({
			buttons:{
				items:[ {
					id:'tree_org_add',
					text:'新增',
					iconCls:'btn btn-primary',
					handler:function(){

						addTreeMenu();
					}
				},{
					id:'tree_org_edit',
					text:'修改',
					iconCls:'btn btn-info',
					handler:function(){

						 updateTreeMenu();
					}
				}, {
					id:'tree_org_delete',
					text:'删除',
					iconCls:'btn btn-danger',
					handler:function(){

						deleteTreeMenu();
					}
				}
				]
			}
		}); 
		
		
    //数据增删查改功能
    var _body= $("body");
 	//新增
     function addTreeMenu(){
 		  var parentId = '';
 		  var parentName ='所有机构';
 		  var pids = '';
 		  if(checkNo != null){
 			  parentId = checkNo.id;
 			  parentName = checkNo.name;
 			  pids = checkNo.pids;
 		  }
    	  _body.createDialog({
	    		  	id:'add_TreeMenu',//id
					contentUrl:'/org/loadAddOrg?pid='+parentId+'&pids='+pids+'&parentName='+encodeURI(parentName),/*弹窗内嵌入页面地址 */
		            title: "新增机构",
		            height:300,
					width:400,
					closeBtn:true, //是否存在右上角关闭按钮
					buttons:{ 		//下方按钮
						items:[{
							id:'closeAddTreeMenu',
							text:'取消',
							iconCls:'btn btn-primary',
							handler:function(){	
								closeModal(add_TreeMenu);//关闭弹窗传弹窗id给closeModal();
							}
						},{
							id:'addAddTreeMenu',
							text:'提交',
							iconCls:'btn btn-success',
							handler:function(){
								$("#defaultForm").bootstrapValidator('validate');//提交验证
				                if ($("#defaultForm").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
									
								//表单提交的方法、比如ajax提交
									$.ajax({
						                cache: true,
						                type: "POST",
						                url:'/org/orgAdd',
						                data:$('#defaultForm').serialize(),// 你的formid
						                async: false,
						                error: function(request) {
						                    alert("Connection error");
						                },
						                success: function(data) {
						                	if(data.success==true){
						                		Notify.notify({msg:"新增成功！",type:'success'});
						                		addNode(data.data);
						                	}else{
						                		Notify.notify({msg:"新增失败！",type:'danger'});
						                	}
						                	closeModal(add_TreeMenu);
						                }
						            });
								
								}
							}	
						}]
				}
    	  });
    	

      }; 
 	
      //修改
	function updateTreeMenu(){
		
		if(checkNo == null){
			Notify.notify({msg:"请先选中节点！",type:'warning'});
			return false;
		}
    		  
  		  _body.createDialog({
    		  	id:'updataTree',//id
				contentUrl:'/org/loadUpdateOrg',/*弹窗内嵌入页面地址 */
				data:{"id":checkNo.id},//选中的id
	            title: "修改机构",
	            height:250,
				width:500,
				closeBtn:true, //是否存在右上角关闭按钮
				buttons:{ 		//下方按钮
					items:[{
						id:'close_updataTree',
						text:'取消',
						iconCls:'btn btn-primary',
						handler:function(){	
							closeModal(updataTree);//关闭弹窗传弹窗id给closeModal();
						}
					},{
						id:'edit_updataTree',
						text:'提交',
						iconCls:'btn btn-success',
						handler:function(){
							//获取表单对象
							$("#upDateForm").bootstrapValidator('validate');//提交验证
				            if ($("#upDateForm").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
								$.ajax({
	          	  	                cache: true,
	          	  	                type: "POST",
	          	  	                url:'/org/orgUpdate',
	          	  	                data:$('#upDateForm').serialize(),// 你的formid
	          	  	                async: false,
	          	  	                error: function(request) {
	          	  	                    alert("出错了");
	          	  	                },
	          	  	                success: function(data) {
		          	  	                if(data.success==true){
					                		
					                		Notify.notify({msg:"修改成功！",type:'success'});
					                		editTree(data.data);//修改节点名称
					                		closeModal(updataTree);//关闭弹窗传弹窗id给closeModal();
					                	}else{
					                		
					                		Notify.notify({msg:"修改失败！",type:'danger'});
					                	}
	          	  	                }
	          	  	            });
							}
						}	
					}]
			}
	  	});
  			
    }; 
    
      //删除
   	 function deleteTreeMenu(){
   		if(checkNo == null){
		
			Notify.notify({msg:"请先选中节点！",type:'warning'});
			return false;
		}
    	  var id = checkNo.id;  
  		  _body.createDialog({
    		  	id:'deleteTree',//id
    		  	content:'确定要删除吗？',
	            title: "删除机构",
				buttons:{ 		//下方按钮
					items:[{
						id:'close_deleteTree',
						text:'取消',
						iconCls:'btn btn-primary',
						handler:function(){	
							closeModal(deleteTree);//关闭弹窗传弹窗id给closeModal();
						}
					},{
						id:'edit_deleteTree',
						text:'提交',
						iconCls:'btn btn-success',
						handler:function(){
							$.ajax({
          	  	                cache: true,
          	  	                type: "POST",
          	  	                url:'/org/deleteOrg?id='+id,
          	  	                data:$('#upDateForm').serialize(),// 你的formid
          	  	                async: false,
          	  	                error: function(request) {
          	  	                    alert("出错了");
          	  	                },
          	  	                success: function(data) {
	          	  	                if(data.success==true){
				                		Notify.notify({msg:"删除成功！",type:'success'});
				                		closeModal(deleteTree);//关闭弹窗传弹窗id给closeModal();
				                		removeNode();//删除节点 
				                	}else{
				                		Notify.notify({msg:"删除失败！",type:'danger'});
				                	}
          	  	                }
          	  	            });
							//closeModal(deleteTree);//关闭弹窗传弹窗id给closeModal();
						}	
					}]
			}
	  	});
      }; 
      
	 }); 
</script>			